enyo.kind({
	name: "AssessmentUI_CreateType",
	kind: "AssessmentUI_PanelBase",
	
	caption: "Assessment Type",
	submitLeftCaption: "Next",
	
	typesCaption: [],
	typesValue: [],
	supportLanCaption: [],
	supportLanValue: [],

	components: [
		{classes:"panel-body", components:[
			{kind:"onyx.Groupbox", classes: "groupbox", components:[
				{kind:"onyx.GroupboxHeader",content:"Assessment Type",classes:"group-header"},
				{classes: "picker", components: [
					{name:"typePickerButton", kind:"onyx.Button", ontap:"pickerButtonClick", content:"Choose Type", classes:"button"},
					{name:"typePickerList", kind:"List", touch: true, count:0, classes:"list", onSetupItem:"setupRow",ontap:"seletctType", components:[
						{name: "item", classes: "item enyo-border-box", components: [
							{name: "types",style:"text-align:center;", content:""},
						]}
					]}
				]},
			]},
			{name:"supportLanGroup", kind:"onyx.Groupbox", classes: "groupbox", components:[
				{kind:"onyx.GroupboxHeader", content:"Supported Language", classes: "group-header"},
				{kind: "Repeater", name: "languageRepeater", classes:"content", onSetupItem: "languageRepeaterSetupRow",components:[
					{name:"supportLanItem", layoutKind: "HFlexLayout", onclick: "itemClick", classes:"clearfix", components: [
						{name:"supportLanContent",style:"float:left;"},
						{kind: "onyx.Checkbox",value: false, onActivate: "checkboxClicked",style:"float:right;"}
					]},
					{tag:"hr"}
				]}
			]},
			{kind: "onyx.Groupbox", classes:"groupbox", components:[
				{name: "descriptionGroupHeader", kind:"onyx.GroupboxHeader",content:"Assessment Type",classes: "group-header"},
				{name:"descriptionContent",content:"",classes: "content"}
			]}
		]},
	],
	create: function() {
		this.inherited(arguments);
		AssessmentUI_Data.current_page = this;
		this.initPanel();
	},
	
	initPanel: function()
	{
		//Restore to the default status
		this.$.typePickerButton.setContent("Choose Type");
		this.$.descriptionContent.setContent("Please choose the assessment type");
		this.typesCaption = [];
		this.typesValue = [];
		this.supportLanCaption = [];
		this.supportLanValue = [];
		
		AssessmentUI_Data.analysisTypePayload = null;
		AssessmentUI_Data.selected_AnalysisTypePayload = null;
		AssessmentUI_Data.payload_AnalysisTypesValue = null;
		
		//hide the groups
		this.$.typePickerList.setStyle("height:0px;");
		//this.$.supportLanList.setStyle("height:0px;");
		this.$.supportLanGroup.setShowing(false);
		//this.$.typePickerList.setShowing(false);
		//call the service
	},
	pickerButtonClick: function(){
		this.$.typePickerButton.setShowing(false);
		//reset type data
		this.resetType();
		//call the service
		var service = new hpit.WebService();
		service.setUrl(AssessmentUI_Config.getServiceUrl_RetrieveAnalysisTypes());
		service.setHeaders(AssessmentUI_Config.getCommonServiceHeader());
		service.call(this, "processResponse");
	},
	processResponse: function(inSender, inResponse) {
		if(inResponse.statusCode === 200) {
			this.showTypeList(inResponse);
			//this.hideScrim();
		}
		else {
			alert("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	showTypeList:function(inResponse){
		AssessmentUI_Data.analysisTypePayload = inResponse;
		
		if(inResponse.payload.length < 10){
			this.height = 28 * inResponse.payload.length;
			this.$.typePickerList.setStyle("height:"+ this.height + "px;");
		}else{
			this.height = 280;//5 count
			this.$.typePickerList.setStyle("height:280px");
		}
		this.$.typePickerList.count = 0;
		for(var i = 0; i < inResponse.payload.length; i++) {
			var payload = inResponse.payload[i];
			this.typesCaption[i] = payload.defaultAnalysisTypeName;
			this.typesValue[i] = payload.analysisTypeCode;
			//this.types.push({caption: payload.defaultAnalysisTypeName, value: payload.analysisTypeCode});
				this.$.typePickerList.count = this.$.typePickerList.count + 1;
		}
		this.$.typePickerList.refresh();
		this.$.typePickerList.scrollToStart();
	},
	setupRow: function(inSender, inEvent){
		// this is the row we're setting up
		var i = inEvent.index;
		// make some mock data if we have none for this row
		if (!this.typesCaption[i]) {
			this.typesCaption[i]= i;
		}
		var n = this.typesCaption[i];
		
		this.$.types.setContent(n);
	},
	
	seletctType: function(){
		var selectedTypeIndex;
		var selectedTypeValue;
		for(var i = 0; i < AssessmentUI_Data.analysisTypePayload.payload.length; i++) {
			if(this.$.typePickerList.isSelected(i)) {
				selectedTypeIndex = i;
			}
		}
		
		selectedTypeValue = this.typesValue[selectedTypeIndex];
		
		this.$.typePickerButton.setContent(this.typesCaption[selectedTypeIndex]);
		this.$.typePickerButton.setShowing(true);
		this.$.typePickerList.setStyle("height:0px;");
		
		AssessmentUI_Data.selected_AnalysisTypePayload = this.getPayloadByCode(selectedTypeValue);
		
		if(AssessmentUI_Data.payload_AnalysisTypesValue != this.typesValue[selectedTypeIndex]){
			AssessmentUI_Data.payload_AnalysisTypesValue = this.typesValue[selectedTypeIndex];
			this.typePickerChanged();		
		}
	},
	
	getPayloadByCode: function(analysisTypeCode){
		for(var i = 0; i < AssessmentUI_Data.analysisTypePayload.payload.length; i++) {
			if(AssessmentUI_Data.analysisTypePayload.payload[i].analysisTypeCode === analysisTypeCode) {
				return AssessmentUI_Data.analysisTypePayload.payload[i];
			}
		}
		return null;
	},
	
	typePickerChanged: function(){
		this.$.descriptionContent.setContent(AssessmentUI_Data.selected_AnalysisTypePayload.defaultAnalysisTypeDescription);
		this.$.supportLanGroup.setShowing(true);

		this.$.languageRepeater.count = AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales.length;
		this.$.languageRepeater.build();
	},
	
	languageRepeaterSetupRow: function(inSender, inEvent) {
		var index = inEvent.index;
		if(AssessmentUI_Data.selected_AnalysisTypePayload == null) return;	//type not selected, maybe not loaded
		if(index < AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales.length ){
			this.supportLanCaption[index] = AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales[index].displayedNameInEnglish;
		}
		if (!this.supportLanCaption[index]) {
			this.supportLanCaption[index]= index;
		}

		var n = this.supportLanCaption[index];
		var rowControl = inEvent.item;
		rowControl.$.supportLanContent.setContent(n);
		this.$.languageRepeater.render();
		return true;
	},
	
	checkboxClicked: function(inSender, inEvent){
		var selectedLanIndex = inEvent.index;
		AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales[selectedLanIndex].checked = inSender.getChecked();
	},

	resetType: function(){
		this.$.typePickerButton.setContent("Choose Type");
		this.$.descriptionContent.setContent("Please choose the assessment type");
		this.typesCaption = [];
		this.typesValue = [];
		
		AssessmentUI_Data.analysisTypePayload = null;
		AssessmentUI_Data.selected_AnalysisTypePayload = null;
		AssessmentUI_Data.payload_AnalysisTypesValue = null;
	}
});